package search;

public class Solution_4 {

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int index1 = 0, index2 = 0, index = 0;
        int m = nums1.length, n = nums2.length;
        int[] nums = new int[m + n];
        while (index1 < m && index2 < n) {
            if (nums1[index1] < nums2[index2]) {
                nums[index++] = nums1[index1];
                index1++;
            } else {
                nums[index++] = nums2[index2];
                index2++;
            }
        }
        if (index1 < m) {
            for (int i = index1; i < m; i++) {
                nums[index++] = nums1[i];
            }
        } else {
            for (int i = index2; i < n; i++) {
                nums[index++] = nums2[i];
            }
        }

        if (nums.length % 2 == 0) {
            int r = nums.length / 2;
            int l = r - 1;
            return (nums[l] + nums[r]) / 2.0;
        } else {
            return nums[nums.length / 2];
        }
    }
}
